package com.example.billw.controller;

import com.example.billw.entity.Provider;
import com.example.billw.mapper.ProviderMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @Description TODO
 * @Author fantasy
 * @Date 2024/7/4 17:49
 **/


@Controller
public class ProviderController {

    @Autowired
    private ProviderMapper providerMapper;


    @GetMapping("/providers")
    public String providerList(@RequestParam(value = "providerName",required = false) String providerName,Model model){
        Provider provider = new Provider();
        if (providerName != null){
            providerName = providerName.trim();
            provider.setProviderName(providerName);
        }
        PageHelper.startPage(2,2);
        List<Provider> providers = providerMapper.getProviders(provider);
//        Page<Provider> page = (Page<Provider>) providers;
        model.addAttribute("providers",providers);
        return "provider/list";
    }
    @GetMapping("/provider")
    public String toAddPage(){
        return "provider/add";
    }
    @GetMapping("/provider/{pid}")
    public String providerDetail(@PathVariable("pid") Integer pid, @RequestParam(value = "type", defaultValue = "view") String type, Model model){
        Provider provider = providerMapper.getProviderById(pid);
        model.addAttribute("provider",provider);
        return "provider/"+type;
    }

    @PostMapping("/provider/update")
    public String providerUpdate(Provider provider){
        providerMapper.updateProvider(provider);
        return "redirect:/providers";
    }
    @PostMapping("/provider/add")
    public String providerAdd(Provider provider){
        providerMapper.addProvider(provider);
        return "redirect:/providers";
    }

    @PostMapping("/provider/delete")
    public String providerDelete(@PathVariable("pid") String pid){
        providerMapper.deleteProvider(pid);
        return "redirect:/providers";
    }
}
